package com.quoc.database;

import java.util.ArrayList;
import java.util.List;

import com.quoc.model.EmployeeModel;
import com.quoc.model.OfficeModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.provider.OpenableColumns;
import android.util.Log;

public class DatabaseSource {
	public static SQLiteDatabase database;
	public CreateDatabases dataHelper;
	public static DatabaseSource instance;

	public static DatabaseSource getInstance(Context context) {
		if (instance == null) {
			Log.i("21106", "create instance");
			instance = new DatabaseSource(context);
		}
		Log.i("21106", "return instance");

		return instance;
	}

	public DatabaseSource(Context context) {
		Log.i("21106", "create data");
		dataHelper = new CreateDatabases(context);
		Open();

	}

	public void Open() throws SQLiteException {
		Log.i("21106", "add data into database");
		database = dataHelper.getWritableDatabase();
	}

	public void Close() {
		database.close();
	}

	// them 1 phong
	public boolean addOffice(ContentValues values) {
		if (database.insert(CreateDatabases.tbOffice, null, values) == -1) {
			return false;
		}
		return true;
	}

	// xoa 1 phongga
	public boolean deleteOffice(int id) {
		if (database.delete(CreateDatabases.tbOffice, CreateDatabases.idOffice
				+ "=" + id, null) == -1) {
			return false;
		}
		return true;
	}

	// sua 1 phong
	public boolean updateOffice(int id, ContentValues values) {
		if (database.update(CreateDatabases.tbOffice, values,
				CreateDatabases.nameOffice + "=" + id, null) == -1) {
			return false;
		}
		return true;
	}

	// readall list phong
	public List<OfficeModel> ReadListOffice() {
		List<OfficeModel> list = new ArrayList<OfficeModel>();

		Cursor c = database.query(CreateDatabases.tbOffice, null, null, null,
				null, null, null);
		c.moveToFirst();
		while (!c.isAfterLast()) {
			Log.i("21106", "4");
			OfficeModel temp = new OfficeModel();
			temp.setMaPhong(Integer.parseInt(c.getString(0)));
			temp.setTenPhong(c.getString(1));
			list.add(temp);
			c.moveToNext();
		}
		return list;
	}

	// add them 1 nhan vien
	public boolean AddEmloyee(ContentValues values) {
		if (database.insert(CreateDatabases.tbEmployee, null, values) == -1) {
			return false;
		}
		return true;
	}

	// xoa 1 nhan vien
	public boolean DeleteEmployee(int id) {
		if (database.delete(CreateDatabases.tbEmployee,
				CreateDatabases.idEmployee + "=" + id, null) == -1) {
			return false;
		}
		return true;
	}

	// sua 1 nhan vien
	public boolean UpdateEmployee(ContentValues values, int ma) {
		if (database.update(CreateDatabases.tbEmployee, values,
				CreateDatabases.idEmployee + "=" + ma, null) == -1) {
			return false;
		}
		return true;
	}

	// read list employee
	public List<EmployeeModel> ReadListEmployee() {
		List<EmployeeModel> eList = new ArrayList<EmployeeModel>();
		EmployeeModel temp;
		Cursor c = database.query(CreateDatabases.tbEmployee, null, null, null,
				null, null, null);
		c.moveToFirst();
		while (!c.isAfterLast()) {
			temp = new EmployeeModel();
			temp.setIdEmployee(Integer.parseInt(c.getString(0)));
			temp.setName(c.getString(1));
			temp.setOfficer(c.getString(2));
			temp.setSex(c.getString(3));
			temp.setOffice(c.getString(4));
			eList.add(temp);
			c.moveToNext();

		}
		return eList;
	}
}
